package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.CmsBuyCredits;
import com.ruoyi.system.service.ICmsBuyCreditsService;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

/**
 * 比特币充值凭证Controller
 *
 * @author ruoyi
 * {@code @date} 2025-04-15
 */
@RestController
@RequestMapping("/system/credits")
public class CmsBuyCreditsController extends BaseController {
    @Autowired
    private ICmsBuyCreditsService cmsBuyCreditsService;

    /**
     * 查询比特币充值凭证列表
     */
    @GetMapping("/list")
    public TableDataInfo list(CmsBuyCredits cmsBuyCredits) {
        startPage();
        List<CmsBuyCredits> list = cmsBuyCreditsService.selectCmsBuyCreditsList(cmsBuyCredits);
        return getDataTable(list);
    }

    /**
     * 导出比特币充值凭证列表
     */
    @Log(title = "比特币充值凭证", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CmsBuyCredits cmsBuyCredits) {
        List<CmsBuyCredits> list = cmsBuyCreditsService.selectCmsBuyCreditsList(cmsBuyCredits);
        ExcelUtil<CmsBuyCredits> util = new ExcelUtil<>(CmsBuyCredits.class);
        util.exportExcel(response, list, "比特币充值凭证数据");
    }

    /**
     * 获取比特币充值凭证详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(cmsBuyCreditsService.selectCmsBuyCreditsById(id));
    }

    /**
     * 新增比特币充值凭证
     */
    @Log(title = "比特币充值凭证", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CmsBuyCredits cmsBuyCredits) {
        return toAjax(cmsBuyCreditsService.insertCmsBuyCredits(cmsBuyCredits));
    }

    /**
     * 修改比特币充值凭证
     */
    @Log(title = "比特币充值凭证", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CmsBuyCredits cmsBuyCredits) {
        return toAjax(cmsBuyCreditsService.updateCmsBuyCredits(cmsBuyCredits));
    }

    /**
     * 删除比特币充值凭证
     */
    @Log(title = "比特币充值凭证", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(cmsBuyCreditsService.deleteCmsBuyCreditsByIds(ids));
    }

    /**
     * 获取比特币汇率
     * @return 结果
     */
    @GetMapping("/getBtb")
    public AjaxResult getBtb(BigDecimal usd) {
        String s = HttpUtils.sendGet("https://www.528btc.com/coin/3008/binance-btc-usdt-usd");
        // 2. 使用Jsoup解析HTML
        Document doc = Jsoup.parse(s);
        // 3. 查找包含汇率的元素 - Bing的汇率结果通常在特定的div中
        // 注意：Bing的HTML结构可能会变化，需要根据实际情况调整选择器
        Elements rateElements = doc.select("i.price_num");
        // 4. 获取美元值
        Element element = rateElements.get(0);
        String text = element.text().replaceAll(",", "");
        BigDecimal price = new BigDecimal(text);
        BigDecimal one = new BigDecimal("1");
        BigDecimal divide = one.divide(price, 7, RoundingMode.HALF_UP);
        BigDecimal multiply = usd.multiply(divide);
        return AjaxResult.success(multiply);
    }


    @PostMapping("/recharge")
    public AjaxResult reviewTheTopUp(@RequestBody CmsBuyCredits cmsBuyCredits) {
        return cmsBuyCreditsService.updateUserIntegral(cmsBuyCredits);
    }
}
